# coding :utf8
import threading  
import time
import urllib2,urllib
import socket
import random
import MySQLdb
from address import main
con=MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234+asdf',db='ipp',port=3306,charset='utf8',use_unicode=True)
portlist=[]
def db1(num,portt,add1,add2):
    cur= con.cursor()
    try :
    	cur.execute('insert into ipp02(num,portt,add1,add2) VALUES '
                    '(%s,%s,%s,%s)',(num,portt,add1,add2))
    	con.commit()
    except :
	pass
    cur.close()
#pin--------------
def pin(IP,por):
    proxy_ip=  {'http':"http://%s:%s"%(IP,por)}
    proxy_support = urllib2.ProxyHandler(proxy_ip)
    opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler)
    urllib2.install_opener(opener)
    url='http://baike.baidu.com/'
    socket.setdefaulttimeout(1.0)
    request = urllib2.Request(url)
    user_agent_list = [
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36',
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
        "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
       ]
   
    user_agent=random.choice(user_agent_list) 
    request.add_header('User-Agent',user_agent)
    try:
        html = urllib2.urlopen(request)
        x='%s:%s'%(IP,por)
        print x
    
    	y=main(IP)
    	db1(IP,por,y[0],y[1])
    except :
	pass
#--------------------------------
class timer(threading.Thread): #The timer class is derived from the class threading.Thread  
    def __init__(self,IP,por):  
        threading.Thread.__init__(self)   
        self.IP=IP
        self.por=por
        self.thread_stop = False  
    def run(self): #Overwrite run() method, put what you want the thread do here     
            threadLock = threading.Lock()
            threadLock.acquire()
            pin(self.IP,self.por)
            threadLock.release()
#ipp2=xxx.xxx.xxx-----------------
def d1(ipp2,por):
    threads1 = []
    for i in range(256):
        ipp1=ipp2+'.%s'%i
        threads1.append(timer(ipp1,por))
    for t in threads1:
        t.start()
    for t in threads1:
        t.join()
#ipp3=xxx.xxx-------------------------
def d2(ipp3,por,k):
    threads2 = []           
    ipp2=ipp3+'.%s'%k
    threads2.append(threading.Thread(target=d1, args=(ipp2,por)))
    for t in threads2:
        t.start()
    for t in threads2:
        t.join()

def f3():
    #list1=[0,20,40,60,80,100,127,140,160,180,200,220,240,256]
    #list1=[0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,180,185,190,195,200,205,210,215,220,225,230,235,240,245,250,256]
    for p in range(8000,10000):
        for i in range(1,256):
	    time.sleep(100)
            for j in range(33,256):
		time.sleep(10)
		print j
                for k in range(256):
                    ipp='%s.%s'%(i,j)
                    d2(ipp,p,k)
       
        
    
if __name__=='__main__':
    print 'aa'
    f3()  
        
        
